<template>
  <div class="home-container">
    <!-- fixed -->
   <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        size="small"
        round
        icon="search"
        type="info"
        to="/search"
      >搜索</van-button>
    </van-nav-bar>
    <van-tabs v-model="active" animated swipeable class="channel-tabs">
      <van-tab
      :title="obj.name"
      v-for="obj in channels"
      :key="obj.id" >
      <!-- 中间组件页面 -->
      <keep-alive>
        <articleList :obj=obj></articleList>
      </keep-alive>
      <!-- /中间组件页面 -->
      </van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hamburger-btn" @click="isPopupShow = true">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 汉堡弹出层· -->
    <van-popup
      v-model="isPopupShow"
      closeable
      close-icon-position="top-left"
      position="bottom"
      :style="{ height: '100%' }">
      <!--  -->
      <channelEdit :myChannel="channels" :active="active" @updateActive="updateActiveFn"></channelEdit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
import channelEdit from './components/channel-edit.vue'
import articleList from './components/article-list.vue'
export default {
  name: 'homeIndex',
  components: {
    articleList,
    channelEdit
  },
  props: {},
  data() {
    return {
      active: 0,
      channels: [],
      isPopupShow: false
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created() {
    this.getUserChannels()
  },
  mounted() {},
  methods: {
    async getUserChannels() {
      try {
        // const { data } = await getUserChannels()
        // // console.log(data)
        // this.channels = data.data.channels
        let channels = []
        if (this.user) {
          // 用户登录状态
          const { data } = await getUserChannels()
          channels = data.data.channels
        } else {
          // 未登录状态
          const locaChannels = getItem('TOUTIAO_CHANNELS')
          if (locaChannels) {
            // 本地有数据
            channels = locaChannels
          } else {
            // 本地没有数据
            const { data } = await getUserChannels()
            channels = data.data.channels
          }
        }
        this.channels = channels
      } catch (error) {
        console.log(error)
        this.$toast('获取用户频道数据失败！')
      }
    },
    // 处理子传父改变active状态
    updateActiveFn(index, isShow) {
      console.log(index)
      // 改变active值
      this.active = index
      // 关闭弹框
      this.isPopupShow = isShow
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  padding-bottom: 100px;
  padding-top: 180px;
   // tabs 标签导航也设置为固定定位
  /deep/ .van-tabs__wrap {
    position: fixed;
    top: 92px;
    z-index: 1;
    left: 0;
    right: 0;
    height: 82px;
  }
   .page-nav-bar {
    background-color: #3296fa;
  }
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/ .channel-tabs {
    .van-tab {
      border-right: 1px solid #EDEFF3;
      min-width: 200px;
      height: 80px;
      .van-tab__text {
        font-size: 27px;
        color: #777777;
      }
    }

    .van-tab--active .van-tab__text{
      color: #333 !important;
    }
    .van-tabs__line {
      width:31px !important;
      height:6px;
      background-color: #3296fa;
      border-radius:3px;
      bottom: 8px;
    }
    .van-tabs__nav {
      padding: 0;
    }
    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background-color: #fff;
      opacity: 0.902;
      i.toutiao {
        font-size: 33px;
      }
      &:before {
        content: "";
        position: absolute;
        left: 0;
        width: 1px;
        height: 100%;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
}
</style>
